﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using WorkbookDesigner.Annotations;

namespace WorkbookDesigner.Addons.SpreadsheetEditor.Model
{
    [UsedImplicitly]
    public static class MissionHelper
    {
        internal static TreeNode GetDirectoryNode(this TreeNodeCollection nodes, DirectoryInfo dirInfo)
        {
            var allDirNode = nodes.OfType<MissionSetModel>();
            MissionSetModel node = allDirNode.FirstOrDefault(x => x.Level == 0 && x.DirectoryInfo.FullName.Equals(dirInfo.FullName));

            if (node != null)
                return node;
            MissionSetModel mset = new MissionSetModel(dirInfo)
            {
                ImageIndex = 0,
                SelectedImageIndex = 0,
                ToolTipText = dirInfo.FullName,//路径唯一
            };
            nodes.Add(mset);
            return mset;
        }

        internal static TreeNode GetFileNode(FileInfo file)
        {
            MissionModel m = new MissionModel(file)
            {
                ImageIndex = 2,
                SelectedImageIndex = 3,
                ToolTipText = file.FullName//唯一凭证
            };

            return m;
        }

        internal static IMission GetMission(this TreeNode node)
        {
            return node as MissionModel;
        }

        internal static IEnumerable<IMission> GetMissions(this TreeNode node)
        {
            return node.Nodes.OfType<IMission>();
        }
        internal static IMissionSet GetMissionSet(this TreeNode node)
        {
            return node as MissionSetModel;
        }
    }
}